package io.bdmc.modules.bss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.bdmc.common.annotation.DataFilter;
import io.bdmc.common.utils.Constant;
import io.bdmc.common.utils.MyPage;
import io.bdmc.modules.bss.entity.DeviceDevice;
import io.bdmc.modules.bss.mapper.DeviceDeviceMapper;
import io.bdmc.modules.bss.service.IDeviceDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备基础信息 服务实现类
 * </p>
 *
 * @author ping
 * @since 2020-06-29
 */
@Service
public class DeviceDeviceServiceImpl extends ServiceImpl<DeviceDeviceMapper, DeviceDevice>
        implements IDeviceDeviceService {
    @Autowired
    DeviceDeviceMapper deviceMapper;

    @Override
    @DataFilter(tableAlias = "d")
    public MyPage<DeviceDevice> mySelectPage(MyPage<DeviceDevice> myPage) {
        return deviceMapper.mySelectPage(myPage);
    }

    @Override
    public List<HashMap<String, Object>> ztreeDeviceList(Long deptId) {

        return deviceMapper.ztreeDeviceList(deptId);
    }

    @Override
    public HashMap<String, Object> deviceFibreAlarmCount(Long deptId) {
        return deviceMapper.deviceFibreAlarmCount(deptId);
    }

    @Override
    public List<DeviceDevice> listAllDevices() {
        return deviceMapper.listAllDevices();
    }

    @Override
    public List<DeviceDevice> listAllFms() {
        List<String> fmsList = new ArrayList<>();
        fmsList.add("IFMS-1000");
        fmsList.add(Constant.FMS);
        fmsList.add(Constant.FMSM);
        fmsList.add(Constant.FMSA);
        LambdaQueryWrapper<DeviceDevice> w1 = Wrappers.lambdaQuery();

        w1.in(DeviceDevice::getDeviceType, fmsList);
        return list(w1);
    }

    @Override
    public List<HashMap<String, Object>> deviceFibreMatchList() {
        return deviceMapper.deviceFibreMatchList();
    }

    @Override
    public DeviceDevice getDeviceDetail(Integer deviceId) {
        return deviceMapper.getDeviceDetail(deviceId);
    }

    @Override
    public DeviceDevice getDeviceBySn(String sn) {
        LambdaQueryWrapper<DeviceDevice> w1 = new LambdaQueryWrapper<>();
        w1.eq(DeviceDevice::getDeviceSn, sn).last("limit 1");
        return deviceMapper.selectOne(w1);
    }

    @Override
    public DeviceDevice getDeviceByIp(String ip) {
        LambdaQueryWrapper<DeviceDevice> w1 = new LambdaQueryWrapper<>();
        w1.eq(DeviceDevice::getIpaddr, ip).last("limit 1");
        return deviceMapper.selectOne(w1);
    }

    @Override
    public List<DeviceDevice> listByType(String deviceType) {
        LambdaQueryWrapper<DeviceDevice> w1 = Wrappers.lambdaQuery();
        w1.eq(DeviceDevice::getDeviceType, deviceType);
        return list(w1);
    }

    /**
     * 根据sn获取单盘
     *
     * @param sn sn
     * @return 单盘
     */
    @Override
    public DeviceDevice getDeviceDiskBySn(String sn) {
        LambdaQueryWrapper<DeviceDevice> w1 = new LambdaQueryWrapper<>();
        w1.eq(DeviceDevice::getDeviceSn, sn).ne(DeviceDevice::getDeviceType, Constant.MSP2000).last("limit 1");
        return deviceMapper.selectOne(w1);
    }

    @Override
    public DeviceDevice selectParentDeviceBySn(String sn) {
        return deviceMapper.selectParentDeviceBySn(sn);
    }

    @Override
    public List<Map<String, Object>> linkTree(Long deptId) {
        return deviceMapper.linkTree(deptId);
    }

    @Override
    public List<Map<String, Object>> deviceTypeSum(Long deptId) {
        return deviceMapper.deviceTypeSum(deptId);
    }

    @Transactional
    @Override
    public Boolean deleteBySlopAndInsertNew(Integer slotNum, Integer parentId, DeviceDevice newDevice) {
        remove(new QueryWrapper<DeviceDevice>().eq("parent_id", parentId).eq("layer_row", slotNum));
        newDevice.setDeviceId(null);
        save(newDevice);
        return true;
    }

    @Override
    public List<DeviceDevice> getFmsAndOtdr() {
        String find = " FIND_IN_SET( device_type,'FMS-M,FMS,FMS-A')";
        final QueryWrapper<DeviceDevice> queryWrapper = new QueryWrapper<DeviceDevice>().apply(find);
        return list(queryWrapper);
    }

    @Override
    public List<DeviceDevice> getOtdrDevice() {
        String find = " FIND_IN_SET( device_type,'FMS-M,FMS-A')";
        final QueryWrapper<DeviceDevice> queryWrapper = new QueryWrapper<DeviceDevice>().apply(find);
        return list(queryWrapper);
    }

    @Override
    public List<DeviceDevice> getByDigestAndType(String deviceDigest, String deviceType) {
        LambdaQueryWrapper<DeviceDevice> w1 = new LambdaQueryWrapper<>();
        w1.eq(DeviceDevice::getDeviceDigest, deviceDigest).eq(DeviceDevice::getDeviceType, deviceType);
        return list(w1);
    }

    @Override
    public List<DeviceDevice> getDirectConnDevice() {
        LambdaQueryWrapper<DeviceDevice> w1 = Wrappers.lambdaQuery();
        w1.eq(DeviceDevice::getDeviceDigest, Constant.EU).ne(DeviceDevice::getDeviceType, Constant.NMS)
                .isNotNull(DeviceDevice::getIpaddr);
        List<DeviceDevice> re = list(w1);
        LambdaQueryWrapper<DeviceDevice> w2 = Wrappers.lambdaQuery();
        w2.eq(DeviceDevice::getDeviceDigest, Constant.FU).eq(DeviceDevice::getDeviceType, Constant.FU);
        re.addAll(list(w2));
        return re;
    }

}
